package com.ruoyi.feike.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.ResultData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.feike.domain.DealerInformation;
import com.ruoyi.feike.domain.TemporaryConditionFollowUp;
import com.ruoyi.feike.domain.vo.TemporaryConditionFollowUpShowVO;
import com.ruoyi.feike.service.ITemporaryConditionFollowUpService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * feikeController
 * 
 * @author zmh
 * @date 2022-07-12
 */
@RestController
@RequestMapping("/feike/up")
@Api(value = "TEMPORARY CONDITION FOLLOW UP",tags = "TEMPORARY CONDITION FOLLOW UP",description = "年审列表下面用")
public class TemporaryConditionFollowUpController extends BaseController
{
    @Autowired
    private ITemporaryConditionFollowUpService temporaryConditionFollowUpService;

    /**
     * 查询feike列表
     */
    //@PreAuthorize("@ss.hasPermi('feike:up:list')")
    @ApiOperation("临时状况跟进列表")
    @PostMapping("/list")
    public ResultData list(@RequestBody List<String> dealerNames)
    {
        //startPage();
        TemporaryConditionFollowUpShowVO vo = new TemporaryConditionFollowUpShowVO();
        if(dealerNames.size() < 1){
            return getResult(null);
        }
        List<TemporaryConditionFollowUp> list = temporaryConditionFollowUpService.selectTemporaryConditionFollowUpList(dealerNames);
        List<String> dealNames = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            dealNames = list.stream().map(TemporaryConditionFollowUp::getDealername).distinct().collect(Collectors.toList());
        }
        vo.setTempList(list);
        vo.setDealNames(dealNames);
        return getResult(vo);
    }

    /**
     * 查询feike列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TemporaryConditionFollowUp temporaryConditionFollowUp)
    {
        startPage();
        List<TemporaryConditionFollowUp> list = temporaryConditionFollowUpService.selectUpList(temporaryConditionFollowUp);
        for (TemporaryConditionFollowUp conditionFollowUp : list) {
            if(conditionFollowUp.getCurrentStatus() !=null && conditionFollowUp.getCurrentStatus().equals("0")){
                conditionFollowUp.setCurrentStatus("Open");
            }else if (conditionFollowUp.getCurrentStatus() !=null && conditionFollowUp.getCurrentStatus().equals("1")){
                conditionFollowUp.setCurrentStatus("Close");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出feike列表
     */
    @ApiOperation("临时状况跟进导出")
    @Log(title = "feike", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TemporaryConditionFollowUp temporaryConditionFollowUp)
    {
        List<TemporaryConditionFollowUp> list = temporaryConditionFollowUpService.selectUpList(temporaryConditionFollowUp);
        for (TemporaryConditionFollowUp conditionFollowUp : list) {
            if(conditionFollowUp.getCurrentStatus() !=null && conditionFollowUp.getCurrentStatus().equals("0")){
                conditionFollowUp.setCurrentStatus("Open");
            }else if (conditionFollowUp.getCurrentStatus() !=null && conditionFollowUp.getCurrentStatus().equals("1")){
                conditionFollowUp.setCurrentStatus("Close");
            }
        }
        ExcelUtil<TemporaryConditionFollowUp> util = new ExcelUtil<TemporaryConditionFollowUp>(TemporaryConditionFollowUp.class);
        return util.exportExcel(list, "condition");
    }

    /**
     * 获取feike详细信息
     */
    //@PreAuthorize("@ss.hasPermi('feike:up:query')")
    @ApiOperation("临时状况跟进列表")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(temporaryConditionFollowUpService.selectTemporaryConditionFollowUpById(id));
    }

    /**
     * 新增feike
     */
    //@PreAuthorize("@ss.hasPermi('feike:up:add')")
    @ApiOperation("临时状况跟进添加")
    @Log(title = "feike", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TemporaryConditionFollowUp temporaryConditionFollowUp)
    {
        return toAjax(temporaryConditionFollowUpService.insertTemporaryConditionFollowUp(temporaryConditionFollowUp));
    }

    /**
     * 修改feike
     */
    //@PreAuthorize("@ss.hasPermi('feike:up:edit')")
    @ApiOperation("临时状况跟进编辑")
    @Log(title = "feike", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TemporaryConditionFollowUp temporaryConditionFollowUp)
    {
        return toAjax(temporaryConditionFollowUpService.updateTemporaryConditionFollowUp(temporaryConditionFollowUp));
    }

    /**
     * 删除feike
     */
    // @PreAuthorize("@ss.hasPermi('feike:up:remove')")
    @ApiOperation("临时状况跟进删除")
    @Log(title = "feike", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        int i = temporaryConditionFollowUpService.deleteTemporaryConditionFollowUpByIds(ids);
        return toAjax(1);
    }

    /**
     * 新增经销商明细表
     */
    //@PreAuthorize("@ss.hasPermi('feike:up:add')")
    @ApiOperation("临时状况跟进添加")
    @Log(title = "feike", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody Map<String, Object> resultMap) {
        try {
            return toAjax(temporaryConditionFollowUpService.saveTcfu(resultMap));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败,原因是:"+e.toString());
        }
    }
}
